<template>
	<div id="bg" class="w-h-100" style="box-sizing: border-box; overflow: hidden">
		<div class="w-h-100 ver-flex">
			<el-card class="box-card" shadow="never" :body-style="{ paddingBottom: '0' }">
				<search ref="searchRef" v-bind="trainSearch" @queryAction="query"> </search>
			</el-card>

			<el-card class="w-h-100 flex-1 box-card" style="margin-top: 16px" shadow="never" :body-style="{ paddingBottom: '0' }">
				<inscroll-table :data-list="list" v-bind="trainTableConfig" :dataCount="Number(totalRows)" :handle-size-change="handleSizeChange" :handle-current-change="handleCurrentChange">
					<template #file="scope">
						<div style="cursor: pointer" @click="fileClick(scope.row.v_FilePath)">{{ scope.row.v_FilePath?.length > 0 ? $t('2KyEk7Ir2Lz3bN9ByjSaE') : $t('66gSt7xsIbieLa59Td1H') }}</div>
					</template>
					<template #operation="scope">
						<div>
							<el-button
								icon="ele-Edit"
								@click="
									() => {
										changePersonAction(scope.row);
									}
								"
								type="primary"
								size="small"
								text
								v-if="$hasPer('trainingImple:post')"
							>
								{{ $t('b4ZGgBxEdO0eAsoy08io') }}
							</el-button>

							<el-button
								icon="ele-Document"
								@click="
									() => {
										detailAction(scope.row);
									}
								"
								type="primary"
								size="small"
								text
							>
								{{ $t('ucC9qxHvkvdh9OGEo1pn') }}
							</el-button>
							<!-- :disabled="scope.row.v_AssessmentResults == 'OK' || scope.row.v_AssessmentResults == 'NG'" -->

							<el-button
								icon="ele-Edit"
								@click="
									() => {
										changeResultAction(scope.row);
									}
								"
								type="primary"
								size="small"
								text
								v-if="$hasPer('trainingImple:change')"
							>
								{{ $t('kiUkiqa3h0HLymgh7eEb') }}
							</el-button>
							<!-- <el-button icon="ele-Folder" v-if="scope.row.v_FilePath?.length > 0" type="success" @click="pushFile(scope.row.v_FilePath)" text size="small">{{
								$t('s4KbtSrTfOzuWyRcHgTxx')
							}}</el-button> -->
						</div>
					</template>
				</inscroll-table>
			</el-card>
		</div>
		<train-form ref="editeResultFormRef" @submit="(param: any) => submitResultHandle(param)"></train-form>
		<edite-train-form @delete-action="query" ref="editeFormRef" @submit="(param: any) => submitHandle(param)" formWidth="70%"></edite-train-form>
		<VModel :fileInfo="fileInfo" :isModelDialog="isModelDialog" @cancelModelClick="cancelModelClick" />
	</div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { search, inscrollTable, trainSearch, trainTableConfig } from './config/index';
import getStaticTableData from '/@/utils/staticGetTableData';
import { getPlanList, postAddTrainEmp, postAddScore } from '/@/api/safety-play/planAPI';

import trainForm from './component/add-train-component.vue';
import editeTrainForm from './component/edite-train-component.vue';
import { IPostPlanType } from '/@/types/ys/index';
import { NORMSTARTTIMEfilter, uploadFiles } from '/@/utils/hook/index';
import { postChangePlan } from '/@/api/safety-play/planAPI';
import eventBus from '/@/utils/eventBus';
import useCurrentInstance from '/@/utils/hook/useCurrentInstance';

import VModel from '../components/v-model.vue';

const { proxy } = useCurrentInstance();
const $t = proxy.$t;

const editeResultFormRef = ref();
const editeFormRef = ref();
const { list, totalRows, handleSizeChange, handleCurrentChange, getData } = getStaticTableData(getPlanList, {}, trainSearch.searchObj);
//附件删除后刷新列表
eventBus.on('getData', getData);
const query = () => {
	getData();
};
let currentChangeData: any;

const changePersonAction = (data: any) => {
	currentChangeData = data;
	editeFormRef.value.change(data);
};

const changeResultAction = (data: any) => {
	currentChangeData = data;
	editeResultFormRef.value.change(currentChangeData);
};
const detailAction = (data: any) => {
	editeResultFormRef.value.detail(data);
};
const submitResultHandle = async (param: any) => {
	let postData: IPostPlanType = {
		V_TrainName: param.v_TrainName,
		N_PlanCourse: currentChangeData.v_CourseName == param.n_PlanCourse ? currentChangeData.n_PlanCourse : param.n_PlanCourse,
		N_TeacherId: currentChangeData.v_EmpName == param.n_TeacherId ? currentChangeData.n_TeacherId : param.n_TeacherId,
		N_Durations: param.n_Durations,
		D_TrainTime: NORMSTARTTIMEfilter(param.d_TrainTime),
		V_TrainAddress: param.v_TrainAddress,
		V_AssessmentMethod: param.v_AssessmentMethod.join(),
		//判断empStr是否为修改前data的name信息,如果是改成oldEmpstrid
		// EmpStr: isChange && formRef.value.oldEmpstr == param.empStr.join() ? formRef.value.oldEmpstrID : changeEmpStr.join(),
		//现在修改时,不能修改培训人员,所以这里暂时穿原值
		EmpStr: editeResultFormRef.value.oldEmpstrID,
		N_isValid: param.n_isValid,
		V_Remake: param.v_Remake,
		N_PlanContentId: currentChangeData.n_PlanContentId,
		N_TrainContentId: currentChangeData.n_TrainContentId,
		D_CompleteTime: NORMSTARTTIMEfilter(param.d_CompleteTime),
		V_AssessmentResults: param.v_AssessmentResults,
	};
	if (editeResultFormRef.value.fileList.length > 0) {
		await uploadFiles(postData.N_TrainContentId, editeResultFormRef.value.fileList);
	}

	postData.n_Id = currentChangeData.n_Id;
	await postChangePlan(postData);
	getData();
};

//提交
const submitHandle = async (param: any) => {
	console.log('editeFormRef.value.personInfoList', editeFormRef.value.personInfoList);
	let postPersonDataArrays = [];
	let postScoreDataArrays = [];
	for (let i = 0; i < editeFormRef.value.personInfoList.length; i++) {
		let person = editeFormRef.value.personInfoList[i];
		if (!person.isServeData) {
			postPersonDataArrays.push(getAddPostObj(person));
		} else if (person.isServeData && !person.disableScore && (person.n_TrainingScores || person.n_TrainingScores == 0)) {
			postScoreDataArrays.push(getChangeScorePostObj(person));
		}
	}
	if (postPersonDataArrays.length > 0) {
		await postAddTrainEmp({
			addTrainEmp: postPersonDataArrays,
		});
	}
	if (postScoreDataArrays.length > 0) {
		await postAddScore({
			updateTrainEmp: postScoreDataArrays,
		});
	}

	console.log('postPersonDataArrays', postPersonDataArrays);
	console.log('postScoreDataArrays', postScoreDataArrays);

	getData();
};
const getAddPostObj = (person: any) => {
	let postPerson = {
		n_TrainingId: currentChangeData.n_Id,
		n_EmpId: person.n_EmpId,
		n_TrainingScores: person.n_TrainingScores,
		c_isQualified: !person.n_TrainingScores && person.n_TrainingScores != 0 ? '' : person.n_TrainingScores >= currentChangeData.course.n_Score ? 'OK' : 'NG',
	};
	return postPerson;
};

const getChangeScorePostObj = (person: any) => {
	let postPerson = {
		n_TrainingId: currentChangeData.n_Id,
		n_Id: person.n_Id,
		n_EmpId: person.n_EmpId,
		n_TrainingScores: person.n_TrainingScores,
		c_isQualified: !person.n_TrainingScores && person.n_TrainingScores != 0 ? '' : person.n_TrainingScores >= currentChangeData.course.n_Score ? 'OK' : 'NG',
	};
	return postPerson;
};

const isModelDialog = ref(false);
const cancelModelClick = () => {
	isModelDialog.value = false;
};
const fileInfo = ref('');
/*** 查看文件 */
const fileClick = (val: any) => {
	if (val.length == 0 || val == null) {
		return;
	}
	isModelDialog.value = true;
	let setObj: any = {};
	setObj.titleInfo = $t('s4KbtSrTfOzuWyRcHgTxx');
	setObj.rectifyListArr = val;
	fileInfo.value = JSON.stringify(setObj);
};
</script>

<!-- scoped属性使style有自己独立的作用域(属性选择器实现 title[data-v-xxx]) -->
<style lang="scss" scoped>
#bg {
	:deep(.el-card__body) {
		height: 100%;
	}
}
</style>
